package org.funtester.performance.books.chapter06.section2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * MySQL基础类
 */
public class MySqlBase {

    /**
     * 数据库连接地址
     */
    public String url;

    /**
     * 用户名
     */
    public String user;

    /**
     * 密码
     */
    public String password;

    /**
     * 数据库连接
     */
    public Connection connection;

    /**
     * Statement对象
     */
    public Statement statement;

    public MySqlBase(String url, String user, String password) {
        this.url = url;
        this.user = user;
        this.password = password;
    }

    /**
     * 初始化资源
     * @throws SQLException
     */
    public void init() throws SQLException {
        this.connection = DriverManager.getConnection(this.url, this.user, this.password);
        this.statement = this.connection.createStatement();
    }

    /**
     * 关闭资源
     * @throws SQLException
     */
    public void close() throws SQLException {
        this.statement.close();
        this.connection.close();
    }

}
